1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import java.sql.{Connection, DriverManager}
object MySQLApp {
def main(args: Array[String]): Unit = {
val url = "jdbc:mysql://localhost:3306/mysql" val username = "root" val password = "root"
var connection:Connection = null try{
// make the connection classOf[com.mysql.jdbc.Driver]
connection = DriverManager.getConnection(url, username, password)
// create the statement, and run the select query val statement = connection.createStatement() val resultSet = statement.executeQuery("select host,user from user") while(resultSet.next()){ val host = resultSet.getString("host") val user = resultSet.getString("user")
println(s"$host, $user") } } catch { case e:Exception => e.printStackTrace() } finally { // free if(connection == null) { connection.close() } }
}
}
|